// Copyright 2011, Vanya Davidenko.
// Используемая кодировка: UTF-8.

#ifndef LIBV_FILE_SFILE_HPP_
#define LIBV_FILE_SFILE_HPP_

#include <string>
#include <libv/stdint.h>
#include <libv/sfile.h>
#include <libv/file/basic_file.h>



namespace libv
{

bool SFile::Open(const ::std::string& file_path,
                 const int open_type,
                 const bool enable_read_ahead_caching /* = true */) const
{
    return BasicFile::BasicOpen(file_path,
                                open_type,
                                enable_read_ahead_caching,
                                0,
                                true);
}



bool SFile::Open(const ::std::string& file_path,
                 const int open_type,
                 const bool enable_read_ahead_caching /* = true */)
{
    return BasicFile::BasicOpen(file_path,
                                open_type,
                                enable_read_ahead_caching,
                                0,
                                false);
}



bool SFile::Write(const fpos64& offset,
                  const void* data,
                  const uint32_t data_size)
{
    if ( !is_opened() || read_only() )
    {
        last_error_ = LIBV_FILE_ERROR_INVALID_HANDLE;
        return false;
    }

    if ( offset < 0 || data == NULL || data_size == 0 )
    {
        last_error_ = LIBV_FILE_ERROR_BAD_ARGUMENTS;
        return false;
    }


    if ( !BasicFile::PreallocateAtLeast(offset + data_size) )
        return false;

    return WriteImpl(offset, data, data_size);
}



bool SFile::Read(const fpos64& offset,
                 void* buffer,
                 const uint32_t buffer_size) const
{
    if ( !is_opened() )
    {
        last_error_ = LIBV_FILE_ERROR_INVALID_HANDLE;
        return false;
    }

    if ( offset < 0 || buffer == NULL || buffer_size == 0 ||
         offset + buffer_size > Size() )
    {
        last_error_ = LIBV_FILE_ERROR_BAD_ARGUMENTS;
        return false;
    }


    return ReadImpl(offset, buffer, buffer_size);
}

}  // libv

#endif  // LIBV_FILE_SFILE_HPP_
